package com._管理系统;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentDemo {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();
        Scanner sc = new Scanner(System.in);
        list.add(new Student("it001", "张三", "28", "广州"));
        list.add(new Student("it002", "李四", "25", "深圳"));
        list.add(new Student("it003", "王五", "24", "东莞"));

        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 退出系统");
            System.out.println("请输入你的选择：");


            String s = sc.nextLine();
            switch (s) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    DeleteStudent(list);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    upadteStudent(list);
                    break;
                case "4":
                    //System.out.println("查看所有学生");
                    showAllStudent(list);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    return;
                default:
                    System.out.println("你的选择无效，请重新选择");
            }
        }


    }

    //添加信息
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学生学号");
            sid = sc.nextLine();

            boolean used = isUsed(sid, list);
            if (used) {
                System.out.println("你输入的学号重复，请重新输入");

            } else {
                break;
            }

        }


        System.out.println("请输入学生姓名");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地");
        String addaess = sc.nextLine();

        Student s = new Student(sid, name, age, addaess);

        list.add(s);
        System.out.println("添加学生成功");

    }

    //查看信息
    public static void showAllStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前没有学生信息，请先添加");
            return;
        }
        System.out.println("学号\t\t\t名字\t\t年龄\t\t\t居住的");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            String sid = s.getSid();
            String name = s.getName();
            String age = s.getAge();
            String address = s.getAddress();
            System.out.println(sid + "\t\t" + name + "\t\t" + age + "\t\t\t" + address);


        }


    }

    //删除信息
    public static void DeleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid = sc.nextLine();

        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (s.getSid().equals(sid)) {
                list.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("你输入的不存在，请重新输入");

    }

    //修改信息
    public static void upadteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要修改的学生学号");
        String sid = sc.nextLine();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (s.getSid().equals(sid)) {
                System.out.println("请输入新的学生姓名");
                s.setName(sc.nextLine());
                System.out.println("请输入新的年龄");
                s.setName(sc.nextLine());
                System.out.println("请输入新的居住地");
                s.setAddress(sc.nextLine());
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("你输入的学号有误，请重新输入");
    }

    public static boolean isUsed(String sid, ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (s.getSid().equals(sid)) {
                return true;
            }
        }
        return false;
    }
}
